<template>
	<view>
		<!-- <view :style="{height: screenTop+'px',backgroundColor:'#f8f8f8'}"></view>
		<uni-nav-bar backgroundColor="#f8f8f8" title="锁具配置">
			<view slot="left">
				<uni-icons @click="backAction()" type="back" size="25"></uni-icons>
			</view>
		</uni-nav-bar> -->
		<j-cell-group v-if="DoorFunctionStatus && DoorFunctionStatus.length > 0">
			<template v-for="(item,index) in settingList">
				<j-cell :key="index"
					v-if="item.status && (item.function !='UnlockDirection' && item.function!='MotorTorque' && item.function!='NormalOpenMode')"
					:title="item.name" @tap="jumpFunction(item,index)">
				</j-cell>
			</template>
			<j-cell v-if="installSetList.length>0" title="安装设置" @tap="InstallSetting()" >
			</j-cell>
		</j-cell-group>
		<empty v-if="DoorFunctionStatus && DoorFunctionStatus.length == 0" :title="'暂无门锁配置'" />
	</view>
</template>

<script>
	import empty from '@/jvss/pagesDevice/components/empty/empty.vue'
	export default {
		components: {
			empty
		},
		data() {
			return {
				settingList: [{
					name: "逗留侦测",
					type: 1,
					function: 'PirConfig'
				}, {
					name: "人体感应",
					type: 10,
					function: 'HumanSensor'
				}, {
					name: "开锁方向设置",
					type: 2,
					function: 'UnlockDirection'
				}, {
					name: "自动反锁设置",
					type: 3,
					function: 'AutoLock'
				}, {
					name: "开锁模式",
					type: 4,
					function: 'UnlockMode'
				}, {
					name: "音量设置",
					type: 5,
					function: 'VolumeControl'
				}, {
					name: "人脸验证提示音",
					type: 6,
					function: 'FaceAlarmTone'
				}, {
					name: "电机扭矩调节",
					type: 7,
					function: 'MotorTorque'
				}, {
					name: "常开模式",
					type: 8,
					function: 'NormalOpenMode'
				}, {
					name: "防撬报警",
					type: 9,
					function: 'DeployDefense'
				}, ],
				device: '',
				DoorFunctionStatus: null,
				installSetList: [],
				screenTop: 0,
				type: 1, //2从验证管理员密码跳转过来的
			}
		},
		onLoad(opt) {
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			this.type = opt.type
			let DoorFunction = JSON.parse(opt.DoorFunction)
			console.log(DoorFunction, 'DoorFunction')
			const sys = uni.getSystemInfoSync();
			this.screenTop = sys.statusBarHeight;
			if (DoorFunction) {
				this.hlandDoorFunction(DoorFunction)
			} else {
				this.doorLockTransparent()
			}
		},
		onUnload() {
			this.backAction()
			uni.removeStorageSync('isInstallverify')
		},
		methods: {
			doorLockTransparent() {
				uni.showLoading({
					title: '获取设备设置...'
				});
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "GetDoorConfig"
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					console.log(result, '门锁设置')
					uni.hideLoading()
					if (result.Ret == 100) {
						this.getdoorLocktability()
					}
				});
			},
			getdoorLocktability() {
				uni.showLoading({
					title: '获取设备能力集...'
				});
				let data = {
					Name: 'DoorFunction'
				};
				let token = this.device.token;
				getApp().globalData.api.getdoorLocktability(data, token, (result) => {
					console.log(result.DoorFunction, 'result.DoorFunction')
					uni.hideLoading()
					let DoorFunction = result.DoorFunction
					this.hlandDoorFunction(DoorFunction)
				});
			},
			hlandDoorFunction(DoorFunction) {
				this.settingList.forEach((item, index) => {
					item.status = DoorFunction[item.function]
				})
				let DoorFunctionStatus = this.settingList.filter(el => el.status);
				this.DoorFunctionStatus = DoorFunctionStatus
				this.$forceUpdate();
				this.installSetList = DoorFunctionStatus.filter(obj => obj.function === "UnlockDirection" || obj
					.function === "MotorTorque" || obj.function === "NormalOpenMode");
				console.log('门锁获取设备能力集:', this.DoorFunctionStatus);
			},
			jumpFunction(item, index) {
				let url = '/jvss/pagesDevice/device/device-live/door-lock/setting/lockSetting/function?token=' + this
					.device.token + '&title=' + item.name + '&type=' + item.type;
				if (item.function == 'UnlockMode') {
					url = '/jvss/pagesDevice/device/device-live/door-lock/setting/lockSetting/functionPrompt?token=' + this
						.device.token + '&title=' + item.name + '&type=' + item.type + '&function=' + item.function
				}
				uni.navigateTo({
					url: url
				})
			},
			// 跳转安装设置
			InstallSetting(){
				let url = '/jvss/pagesDevice/device/device-live/door-lock/setting/lockSetting/function?token=' + this.device.token + '&title=' +'安装设置'  +'&installSetList='+ JSON.stringify(this.installSetList)
				let isInstallverify = uni.getStorageSync('isInstallverify')
				if(isInstallverify){
					uni.navigateTo({
						url: url
					})
				}else{
					this.jumpToVerify()
				}
			},
			// 跳转到验证页面
			jumpToVerify() {
				uni.navigateTo({
					url: `/jvss/pagesDevice/device/device-live/door-lock/userManage/verifyPassword?type=3&device=${encodeURIComponent(JSON.stringify(this.device))}&installSetList=${JSON.stringify(this.installSetList)}`
				})
			},
			// 发送End结束流程
			backAction() {
				uni.showLoading({
					mask: true
				});
				let data = {
					Name: 'OPDoorLockProCmd',
					OPDoorLockProCmd: {
						Cmd: "SyncDoorStatus",
						DoorStatus: {
							Status: 'End'
						},
						SessionID: 0x00000001
					}
				};
				let token = this.device.token;
				getApp().globalData.api.doorLockTransparent(data, token, (result) => {
					console.log('发送End', result);
					uni.hideLoading()
					if (result.data.code == 2000) {
						uni.setStorageSync('StartFlag', {})
					}
				}, false);
			}
		}
	}
</script>

<style lang="scss">
	.uni-navbar__header {
		background-color: #f8f8f8;
	}
</style>
